/*
* 不指定文件名,file传字符串,指定文件名传对象{path:'',name:''},多文件上传数组形式["","",""],多文件不支持自定义文件名
* 三个回调函数,完成上传回调,错误回调,上传进度回调
*
*
* */

function aliUpload (file, completeCallback, errCallback, progressCallback) {
  var filePath, fileName;
  var type;//是否是单文件上传
  var uploadFileList = [];
  if (Object.prototype.toString.call(file) == '[object Object]') {
    type = 1;
    filePath = file.path;
    fileName = file.name;
  } else if (Object.prototype.toString.call(file) == '[object String]') {
    type = 1;
    filePath = file;
    fileName = new Date().getTime() + '';
  } else if (Object.prototype.toString.call(file) == '[object Array]') {
    type = 2;
    filePath = file;
    fileName = [];
    for (var i = 0; i < file.length; i++) {
      fileName.push(new Date().getTime() + '' + Math.floor(Math.random() * 10000 + 1));
    }
  } else {
    console.log('请检查参数是否正确');
    return;
  }
  if (type == 1) {
    var aliOSS = api.require('aliOSS');
    aliOSS.init({
      endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
      accessKeyId: 'LTAIanA2hFK9zAy1',
      accessKeySecret: 'bjTsXJwUJdKa2ggCr0RkEdbTKtudsM',
      bucketName: 'develop-kingsuper'
    }, function (ret, err) {
      console.log(JSON.stringify(ret));
      console.log(JSON.stringify(err));
      if (ret) {
        aliOSS.upload({
          file: filePath,
          name: fileName
        }, function (ret, err) {
          if (ret.oper == 'progress') {
            if (progressCallback) {
              progressCallback(ret);
            }
            console.log('上传过程中' + JSON.stringify(ret));
          } else if (ret.oper == 'complete') {
            completeCallback(fileName);
            console.log('已完成上传' + JSON.stringify(ret));
          }
          if (err) {
            console.log('上传失败' + JSON.stringify(ret));
            errCallback(err);
          }
        });
      }
      if (err) {
        console.log(err);
      }
    });
  } else {
    var aliOSS = api.require('aliOSS');
    aliOSS.init({
      endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
      accessKeyId: 'LTAIanA2hFK9zAy1',
      accessKeySecret: 'bjTsXJwUJdKa2ggCr0RkEdbTKtudsM',
      bucketName: 'develop-kingsuper'
    }, function (ret, err) {
      console.log(JSON.stringify(ret));
      console.log(JSON.stringify(err));
      if (ret) {
        for (var i = 0; i < fileName.length; i++) {
          (function (i) {
            aliOSS.upload({
              file: filePath[i],
              name: fileName[i]
            }, function (ret, err) {
              if (ret.oper == 'progress') {

              } else if (ret.oper == 'complete') {
                LoadingShow();
                uploadFileList.push(fileName[i]);
                if (uploadFileList.length == fileName.length) {
                  completeCallback(uploadFileList);
                  LoadingClose();
                }
              }
              if (err) {
                console.log('上传失败' + JSON.stringify(ret));
                errCallback(err);
              }
            });
          })(i);
        }
      }
      if (err) {
        console.log(err);
      }
    });
  }
}

















/*
* 不指定文件名,file传字符串,指定文件名传对象{path:'',name:''},多文件上传数组形式["","",""],多文件不支持自定义文件名
* 三个回调函数,完成上传回调,错误回调,上传进度回调
*
*
* */

function aliUpload22 (file, completeCallback, errCallback, progressCallback) {
  var filePath, fileName;
  var type;//是否是单文件上传
  var uploadFileList = [];
  if (Object.prototype.toString.call(file) == '[object Object]') {
    type = 1;
    filePath = file.path;
    fileName = file.name;
  } else if (Object.prototype.toString.call(file) == '[object String]') {
    type = 1;
    filePath = file;
    fileName = new Date().getTime() + '';
  } else if (Object.prototype.toString.call(file) == '[object Array]') {
    type = 2;
    filePath = file;
    fileName = [];
    for (var i = 0; i < file.length; i++) {
      fileName.push(new Date().getTime() + '' + Math.floor(Math.random() * 10000 + 1));
    }
  } else {
    console.log('请检查参数是否正确');
    return;
  }
  if (type == 1) {
    var aliOSS = api.require('aliOSS');
    aliOSS.init({
      endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
      accessKeyId: 'LTAIanA2hFK9zAy1',
      accessKeySecret: 'bjTsXJwUJdKa2ggCr0RkEdbTKtudsM',
      bucketName: 'develop-kingsuper'
    }, function (ret, err) {
      console.log(JSON.stringify(ret));
      console.log(JSON.stringify(err));
      if (ret) {
        aliOSS.upload({
          file: filePath,
          name: fileName+'.mp3'
        }, function (ret, err) {
          if (ret.oper == 'progress') {
            if (progressCallback) {
              progressCallback(ret);
            }
            console.log('上传过程中' + JSON.stringify(ret));
          } else if (ret.oper == 'complete') {
            completeCallback(fileName);
            console.log('已完成上传' + JSON.stringify(ret));
          }
          if (err) {
            console.log('上传失败' + JSON.stringify(ret));
            errCallback(err);
          }
        });
      }
      if (err) {
        console.log(err);
      }
    });
  } else {
    var aliOSS = api.require('aliOSS');
    aliOSS.init({
      endpoint: 'http://oss-cn-hangzhou.aliyuncs.com',
      accessKeyId: 'LTAIanA2hFK9zAy1',
      accessKeySecret: 'bjTsXJwUJdKa2ggCr0RkEdbTKtudsM',
      bucketName: 'develop-kingsuper'
    }, function (ret, err) {
      console.log(JSON.stringify(ret));
      console.log(JSON.stringify(err));
      if (ret) {
        for (var i = 0; i < fileName.length; i++) {
          (function (i) {
            aliOSS.upload({
              file: filePath[i],
              name: fileName[i]+'.mp3'
            }, function (ret, err) {
              if (ret.oper == 'progress') {

              } else if (ret.oper == 'complete') {
                uploadFileList.push(fileName[i]);
                if (uploadFileList.length == fileName.length) {
                  completeCallback(uploadFileList);
                }
              }
              if (err) {
                console.log('上传失败' + JSON.stringify(ret));
                errCallback(err);
              }
            });
          })(i);
        }
      }
      if (err) {
        console.log(err);
      }
    });
  }
}
